package com.dl7.mvp.video.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.dl7.mvp.CommonConstant;
import com.dl7.mvp.base.BaseFragment;
import com.dl7.mvp.base.baseadapter.BaseRecyclerViewAdapter;
import com.dl7.mvp.mvp.databinding.BaseRecyclerviewVerticalBinding;
import com.dl7.mvp.video.bean.VideoCategoryList;
import com.dl7.mvp.mvp.R;
import com.dl7.mvp.video.adapter.VideoAlbumListAdapter;

import java.util.ArrayList;
import java.util.List;


public class VideoAlbumListFragment extends BaseFragment<BaseRecyclerviewVerticalBinding> {
    public VideoAlbumListFragment() {
        // Required empty public constructor
    }

    @Override
    public int getContentViewResID() {
        return R.layout.base_recyclerview_vertical;
    }

    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initRecyclerView();
        if (getArguments() != null) {
            int id = getArguments().getInt(CommonConstant.CATEGORY_ID);
            List<VideoCategoryList.CategoryBean.AlbumBean> list = ((VideoMainFragment) getParentFragment()).getAlbumsList(id);
            setData(list);
        }
    }

    private void initRecyclerView() {
        bindingView.recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
    }

    public static VideoAlbumListFragment newInstance(int categoryID) {
        VideoAlbumListFragment fragment = new VideoAlbumListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(CommonConstant.CATEGORY_ID, categoryID);
        fragment.setArguments(bundle);
        return fragment;
    }

    private void setData(List<VideoCategoryList.CategoryBean.AlbumBean> albumsList) {
        VideoAlbumListAdapter adapter = new VideoAlbumListAdapter();
        adapter.addDatas((ArrayList<VideoCategoryList.CategoryBean.AlbumBean>) albumsList);
        bindingView.recyclerView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        adapter.setOnItemClickListener(new MyOnItemClickListener());
    }

    class MyOnItemClickListener implements BaseRecyclerViewAdapter.OnItemClickListener<VideoCategoryList.CategoryBean.AlbumBean> {
        @Override
        public void onItemClick(int position, VideoCategoryList.CategoryBean.AlbumBean data) {
            // TODO
            VideoPlayerActivity.launch(getContext(), data);
        }
    }
}
